﻿@{
    ViewData["Title"] = "Add";
    Layout = null;
}

<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />

<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>录入供应商</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
        }

        .container {
            width: 100%;
            max-width: 600px;
            margin: auto;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }

        th,
        td {
            border: 1px solid #ddd;
            padding: 10px;
        }

        th {
            background-color: #f4f4f4;
            text-align: left;
        }

        label {
            display: block;
            margin-bottom: 5px;
            color: red;
            font-weight: bold;
        }

        input[type="text"],
        select,
        input[type="number"] {
            width: calc(100% - 22px);
            padding: 8px;
            margin-bottom: 10px;
        }

        .buttons {
            text-align: right;
        }

            .buttons button {
                background-color: #007bff;
                color: white;
                border: none;
                padding: 8px 16px;
                margin-left: 5px;
                cursor: pointer;
                border-radius: 4px;
            }

                .buttons button:hover {
                    background-color: #0056b3;
                }
    </style>
</head>

<body>

    <div class="container">
        <h2>录入</h2>
        <form id="stockForm">
            <table>
                <tr>
                    <td>
                        <label for="Name">盘点人</label>
                        <input type="text" id="Name" required placeholder="请输入盘点人">
                    </td>
                    <td>
                        <label for="Product">盘点商品</label>
                        <select id="Product" required>
                            <option value="">请选择商品</option>
                            <!-- 动态加载商品选项 -->
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="bookQuantity">账面数量</label>
                        <input type="number" id="bookQuantity" required placeholder="请输入账面数量">
                    </td>
                    <td>
                        <label for="sdate">状态</label>
                        <input type="radio" name="sdate" value="1" checked> 正常
                        <input type="radio" name="sdate" value="2"> 异常
                    </td>
                </tr>
            </table>

            <div class="buttons">
                <button type="submit">确定</button>
                <button type="button" onclick="reset()">关闭</button>
            </div>
        </form>
    </div>

    <script src="~/libs/jquery/jquery.js"></script>
    <script src="~/libs/layui-v2.5.6/layui/layui.all.js"></script>

    <script>
        $(document).ready(function () {
            // 加载商品数据
            function loadProducts() {
                $.ajax({
                    url: '/TakeStock/GetProductDetail', // 请替换为你的API地址
                    type: 'GET',
                    success: function (response) {
                        var productSelect = $('#Product');
                        $.each(response.data, function (index, product) {
                            productSelect.append('<option value="' + product.productName + '">' + product.productName + '</option>');
                        });
                    },
                    error: function (xhr, status, error) {
                        console.error('加载商品数据失败: ' + error);
                    }
                });
            }

            loadProducts();

            $('#stockForm').on('submit', function (event) {
                event.preventDefault();

                if (this.checkValidity()) {
                    console.log("提交表单");

                    $.ajax({
                        url: '/TakeStock/AddTakeStock',
                        type: 'post',
                        data: {
                            name: $('#Name').val(),
                            good: $('#Product').val(),
                            bookQuantity: parseInt($('#bookQuantity').val()),
                            sdate: parseInt($('input[name="sdate"]:checked').val()),
                            countQuantity:0
                        },
                        success: function (res) {
                            console.log(res);
                            if (res == 1) {
                                layer.msg("提交成功", { icon: 1, time: 1000 }, function () {
                                    reset();
                                });
                            } else {
                                layer.msg("提交失败", { icon: 2, time: 1000 });
                            }
                        }
                    });
                } else {
                    console.log("请填写所有必填字段");
                }
            });
        });

        function reset() {
            console.log("返回");
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        }
    </script>

</body>

</html>
